import datetime
import os.path
import sys
from PIL import ImageGrab
#创建一个浏览器对象
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


class Driver:
    driver = ""
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(service = Service(ChromeDriverManager().install()),options = options)
        self.driver.implicitly_wait(5)

    def getScreeShot(self):
        #创建屏幕截图
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        if not os.path.exists("../images/" + dirname):
            os.mkdir("../images/" + dirname)
        filename = sys._getframe().f_back.f_code.co_name + "-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
        self.driver.save_screenshot("../images/"+dirname+"/"+filename)

    def get_alert_image(self):
        '''
        截图包含弹窗的页面
        后续需要截图弹窗的时候调用这个方法
        :return:
        '''
        # 创建屏幕截图
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        if not os.path.exists("../images/" + dirname):
            os.mkdir("../images/" + dirname)
        filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime(
            "%Y-%m-%d-%H%M%S") + ".png"
        screenshot = ImageGrab.grab()
        screenshot.save("../images/"+dirname+"/"+filename)

ChatDriver = Driver()